package org.jeecg.modules.basicWarehouse.service.impl;

import org.jeecg.modules.basicWarehouse.entity.BasicWarehouse;
import org.jeecg.modules.basicWarehouse.entity.ConnectionPosition;
import org.jeecg.modules.basicWarehouse.mapper.ConnectionPositionMapper;
import org.jeecg.modules.basicWarehouse.mapper.BasicWarehouseMapper;
import org.jeecg.modules.basicWarehouse.service.IBasicWarehouseService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 仓库
 * @Author: jeecg-boot
 * @Date:   2025-03-12
 * @Version: V1.0
 */
@Service
public class BasicWarehouseServiceImpl extends ServiceImpl<BasicWarehouseMapper, BasicWarehouse> implements IBasicWarehouseService {

	@Autowired
	private BasicWarehouseMapper basicWarehouseMapper;
	@Autowired
	private ConnectionPositionMapper connectionPositionMapper;
	
	@Override
	@Transactional
	public void saveMain(BasicWarehouse basicWarehouse, List<ConnectionPosition> connectionPositionList) {
		basicWarehouseMapper.insert(basicWarehouse);
		if(connectionPositionList!=null && !connectionPositionList.isEmpty()) {
			for(ConnectionPosition entity:connectionPositionList) {
				//外键设置
				entity.setWarehouseId(basicWarehouse.getId());
				entity.setWarehouseName(basicWarehouse.getName());
				connectionPositionMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(BasicWarehouse basicWarehouse,List<ConnectionPosition> connectionPositionList) {
		basicWarehouseMapper.updateById(basicWarehouse);
		
		//1.先删除子表数据
		connectionPositionMapper.deleteByMainId(basicWarehouse.getId());
		
		//2.子表数据重新插入
		if(connectionPositionList!=null && !connectionPositionList.isEmpty()) {
			for(ConnectionPosition entity:connectionPositionList) {
				//外键设置
				entity.setWarehouseId(basicWarehouse.getId());
				entity.setWarehouseName(basicWarehouse.getName());
				connectionPositionMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		connectionPositionMapper.deleteByMainId(id);
		basicWarehouseMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			connectionPositionMapper.deleteByMainId(id.toString());
			basicWarehouseMapper.deleteById(id);
		}
	}
	
}
